package thread.wang_bao_ling.c31;

public class HandleWeb {


    Object handleWebReq(){
        long id=System.currentTimeMillis();
        //创建一消息
        Message message = new  Message(id,"{...}");
        //创建GuardedObject实例
        GuardedObject<Message> go=  GuardedObject.create(id);
        //发送消息
        send(message);
        //等待MQ消息
        Message r = go.get(t->t != null);
        return r;
    }

    public void send(Message message){
        //新建一个线程模拟消息队列处理消息
        new Thread(()->{
            try {
                Thread.currentThread().sleep(3000);
                onMessage(message);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }


    void onMessage(Message message){
        //唤醒等待的线程
        GuardedObject.fireEvent(message.id, message);
    }
}
